package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    private Executor executor;


    /**
     * 根据专辑ID汇总详情页所需参数
     *
     * @param albumId
     * @return
     */
    /*@Override
    public ItemVo getItem(Long albumId) {
        //1.创建ItemVo对象
        ItemVo itemVo = new ItemVo();

        //2.远程调用专辑服务：根据专辑ID查询专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            log.info("获取专辑信息异步任务:{}", albumId);
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑{}不存在", albumId);
            itemVo.setAlbumInfo(albumInfo);
            return albumInfo;
        }, executor);

        //3.远程调用专辑服务：根据专辑所属分类ID查询分类信息
        CompletableFuture<Void> baseCategoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView categoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            Assert.notNull(categoryView, "分类{}不存在", albumInfo.getCategory3Id());
            itemVo.setBaseCategoryView(categoryView);
        }, executor);

        //4.远程调用用户服务：根据专辑所属主播ID查询主播信息
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
            Assert.notNull(userInfoVo, "用户{}不存在", albumInfo.getUserId());
            itemVo.setAnnouncer(userInfoVo);
        }, executor);

        //5.远程调用专辑服务：根据专辑ID查询专辑统计信息
        CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
            log.info("获取专辑统计异步任务:{}", albumId);
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑统计{}不存在", albumId);
            itemVo.setAlbumStatVo(albumStatVo);
        }, executor);


        //6.组合异步任务
        CompletableFuture.allOf(
                        albumInfoCompletableFuture,
                        baseCategoryCompletableFuture,
                        announcerCompletableFuture,
                        albumStatVoCompletableFuture
                ).orTimeout(1, TimeUnit.SECONDS)
                .join();

        //7.响应ItemVo对象
        return itemVo;
    }*/

    @Autowired
    private RedissonClient redissonClient;


    /**
     * 根据专辑ID汇总详情页所需参数
     *
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getItem(Long albumId) {
        //0.采用布隆过滤器解决缓存穿透问题
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        boolean contains = bloomFilter.contains(albumId);
        if(!contains){
            throw new GuiguException(404, "专辑不存在");
        }

        //1.创建Map对象用于封装详情页数据
        Map<String, Object> map = new ConcurrentHashMap<>();

        //2.远程调用专辑服务：根据专辑ID查询专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            log.info("获取专辑信息异步任务:{}", albumId);
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑{}不存在", albumId);
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, executor);

        //3.远程调用专辑服务：根据专辑所属分类ID查询分类信息
        CompletableFuture<Void> baseCategoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView categoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            Assert.notNull(categoryView, "分类{}不存在", albumInfo.getCategory3Id());
            map.put("baseCategoryView", categoryView);
        }, executor);


        //4.远程调用用户服务：根据专辑所属主播ID查询主播信息
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
            Assert.notNull(userInfoVo, "用户{}不存在", albumInfo.getUserId());
            map.put("announcer", userInfoVo);
        }, executor);

        //5.远程调用专辑服务：根据专辑ID查询专辑统计信息
        CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
            log.info("获取专辑统计异步任务:{}", albumId);
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑统计{}不存在", albumId);
            map.put("albumStatVo", albumStatVo);
        }, executor);


        //6.组合异步任务
        CompletableFuture.allOf(
                        albumInfoCompletableFuture,
                        baseCategoryCompletableFuture,
                        announcerCompletableFuture,
                        albumStatVoCompletableFuture
                ).orTimeout(1, TimeUnit.SECONDS)
                .join();

        //7.响应Map对象
        return map;
    }
}
